﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MinistryStandardPlatformEntity;


namespace MinistryStandardPlatformWeb.Controllers
{
    public class DictionaryController : Controller
    {
        //
        // GET: /Dictionary/
        private MSPDataContext context=new MSPDataContext();

       public ActionResult Index(string id)
        {
           var dic = context.Dictionary_Detail.Where(u => u.TypeID == id).Select(u => u);
           ViewData.Model = dic;
           ViewBag.id = id;
           return PartialView();
        }

       public ActionResult Create(string id)
       {
           var dicName = context.DictionaryType.Where(u => u.DictypeId == id).Select(u => u.DictypeName).FirstOrDefault().ToString();
           ViewBag.dicname = dicName;
           ViewBag.id = id;
           return View();
       }

       [HttpPost]
       public ActionResult Create(Dictionary_Detail dic_detail) 
       {
           if (ModelState.IsValid)
           {
               
               try
               {
                   ViewBag.id = dic_detail.TypeID;
                   var chk_dicname = context.Dictionary_Detail.Where(p => p.DicName == dic_detail.DicName).FirstOrDefault();
                   if (chk_dicname != null)
                   {
                       
                       ViewBag.Message = "已存在该字典名称，添加失败";
                       //return View(dic_detail);
                   }
                   else
                   {
                       dic_detail.DicID = "DICT" + CommonFunctions.GetDateTimeStrAnd4BitsRandom();
                       context.Dictionary_Detail.InsertOnSubmit(dic_detail);
                       context.SubmitChanges();
                       ViewBag.Message = "添加成功";
                   }
               }
               catch
               {
                   ViewBag.Message = "添加失败";
               }
           }
           else
           {
               ViewBag.Message = "添加失败";
           }
           return View(dic_detail);
       }


       [HttpPost]
       public ActionResult Delete(string id)
       {
           var dic_detail = context.Dictionary_Detail.Single(s => s.DicID == id);
           context.Dictionary_Detail.DeleteOnSubmit(dic_detail);
           context.SubmitChanges();
           return Json(new {success=true});
           //return new HttpStatusCodeResult(System.Net.HttpStatusCode.OK);
       }

       //public ActionResult Edit()
       //{
       //    return View();
       //}

       public ActionResult Edit(string id)
       {
           string[] num = id.Split(',');
           string dicid = num[0];
           string name = num[1];
           string memo = num[2];
           string type = num[3];
           var m_dicname = context.DictionaryType.Where(u => u.DictypeId == type).Select(u => u.DictypeName).FirstOrDefault().ToString();
           ViewBag.dicname = m_dicname;
           ViewBag.id = type;
           ViewBag.dicid = dicid;
           var dic = context.Dictionary_Detail.Single(u => u.DicID == dicid);
           return View(dic);
       }

       [HttpPost]
       public ActionResult Edit(FormCollection form)
       {
            try
            {
                ViewBag.dicid = form["DicID"];
                ViewBag.id = form["TypeID"];
                var dicToUpdate = context.Dictionary_Detail.SingleOrDefault(u => u.DicID == form["DicID"]);

                var chk_dicname = context.Dictionary_Detail.Where(p => p.DicName == form["DicName"]).FirstOrDefault();
                if (chk_dicname == null || chk_dicname.DicID == form["DicID"])
                
                {
                    //ViewBag.id = dicToUpdate.TypeID;
                    
                    UpdateModel(dicToUpdate, form.ToValueProvider());
                    context.SubmitChanges();
                    ViewBag.Message = "修改成功";
                    return View(dicToUpdate);

                }
                else
                {
                    ViewBag.Message = "字典名称已存在，修改失败";
                    return View(dicToUpdate);
                }     
            }
            catch (System.Exception ex)
            {
                ViewBag.Message = "修改失败," + ex.Message;
                return View(new Dictionary_Detail());
            }
       }
    }
}
